#!/bin/bash
# -*- encoding: utf8 -*-

from config.config import reviewfile
from config.config import sqllimit
from BModel.globalfun import OrderCrontall

"""处理老数据函数"""
def OldDataInsert(db):
    oldsql = "SELECT ReviewID FROM `bw_ctrip_review_cn` WHERE WritingDate >= '2017-10-26 00:00:00' AND (RatingPosit+RatingCostBenefit+RatingService+RatingRoom)<=16"
    print oldsql
    all = db.bmquery(oldsql,'ctripo')
    reviewlist = []
    if True == all['status']:
        for v in all['data']:
            reviewlist.append(v['ReviewID'])
    else:
        return False

    revirenum = len(reviewlist)
    if 0 < revirenum:
        """目前 100000 分页"""
        page = int(revirenum/sqllimit)+1
        i = 0
        for i in range(0,page):
            start = i*sqllimit+1
            end = sqllimit*(i+1)
            tmplist = reviewlist[start:end]
            insertsql,stip = "",""
            sqlreviewlist,sqlstip = "",""
            for v in tmplist:
                sqlreviewlist = str(sqlreviewlist) +sqlstip + str(v)
                sqlstip = ","
            if sqlreviewlist:
                print reviewlist
                print sqlreviewlist
                oldsql = "SELECT ReviewID,OrderID FROM `bw_ctrip_review_cn`WHERE ReviewID IN (%s)"%(sqlreviewlist)
                reviewdata = db.bmquery(oldsql,'ctripo')
                if True == reviewdata['status']:
                    for v in reviewdata['data']:
                        insertsql = insertsql+stip +"(%s,'%s',1)"%(v['ReviewID'], OrderCrontall(v['OrderID']))
                        stip = ","
            if insertsql:
                hotelsql = "INSERT INTO BW_Order_Review_List(`ReviewID`,`Order`,`SourceID`) VALUES%s"%(insertsql)
                print hotelsql
                # db.bmquery(hotelsql,'order')


def NewDataInsert(db):
    newsql = "SELECT ID FROM `bw_ctrip_review_cn_v2` WHERE WritingDate >= %s and Rating <= 4 "%('1483200000000')
    all = db.bmquery(newsql,'ctripn')
    reviewlist = []
    if True == all['status']:
        for v in all['data']:
            reviewlist.append(v['ID'])
    else:
        return False
    maxid = 0
    revirenum = len(reviewlist)
    if 0 < revirenum:
        page = int(revirenum/sqllimit)+1
        i = 0
        for i in range(0,page):
            start = i*sqllimit+1
            end = sqllimit*(i+1)
            """修改第一条漏掉问题"""
            if i == 0:
                start = 0
            tmplist = reviewlist[start:end]
            insertsql,stip = "",""
            sqlreviewlist,sqlstip = "",""
            for v in tmplist:
                sqlreviewlist = str(sqlreviewlist) +sqlstip + str(v)
                sqlstip = ","
            if sqlreviewlist:
                oldsql = "SELECT ID,ReviewID,Rating,OrderID FROM `bw_ctrip_review_cn_v2` WHERE ID IN (%s)"%(sqlreviewlist)
                reviewdata = db.bmquery(oldsql,'ctripn')
                if True == reviewdata['status']:
                    for v in reviewdata['data']:
                        maxid = v['ID']
                        insertsql = insertsql+stip +"(%s,'%s',1)"%(v['ReviewID'], OrderCrontall(v['OrderID']))
                        stip = ","
            if insertsql:
                hotelsql = "INSERT INTO BW_Order_Review_List(`ReviewID`,`Order`,`SourceID`) VALUES%s"%(insertsql)
                db.bmquery(hotelsql,'order')

    if 0 < maxid:
        fp = open(reviewfile,'w+')
        fp.write(str(maxid))
        fp.close()

